<?php 
/**
 * @version        $Id$
 * @author         master@xuewl.com
 * @copyright      Copyright (c) 2007 - 2013, Chongqing Zero Technology Co. Ltd.
 * @link           http://www.xuewl.com
**/
function check_answer_status($aid = 0, $uid = 0) {
	$aid = (int) $aid;
	$uid = (int) ($uid < 1) ? cookie('_userid') : $uid;
	$models = getcache('model', 'commons');
	$model = $models[8];
	if($uid < 1) return TRUE;
	if(!$model || $aid < 1) return FALSE;
	$system_info = M($model['tablename'])->getById($aid);
    $log_map = array();
    $log_map['uid'] = $uid;
    $log_map['id_encode'] = id_encode(8, $aid);
    if($system_info['repeat_reward'] == 0 && M($model['tablename'].'_log')->where($log_map)->count()) {
        return FALSE;
    } else {
    	return TRUE;
    }
}

/* 获取打码账户信息 */
function get_captcha_info($aid = 0, $uid = 0) {
    $aid = (int) $aid;
    $uid = (int) $uid;
    $uid = (int) ($uid < 1) ? cookie('_userid') : $uid;
    if($aid < 1 || $uid < 1) return FALSE;
    $sql_map = array('uid' => $uid,'aid' => $aid);
    return M('DamaAccount')->where($sql_map)->find();
}

function get_captcha_list($aid = 0, $uid = 0) {
    $aid = (int) $aid;
    $uid = (int) ($uid < 1) ? cookie('_userid') : $uid;

    $timestamp = NOW_TIME;
    $day_time = array();
    for ($i=0; $i < 14; $i++) { 
        $day_time[] = strtotime(dgmdate($timestamp - (86400 * $i), 'Y-m-d'));
    }
    if($aid && $uid) {
        $sqlmap = $lists = $result = array();
        $sqlmap['yard_time'] = array("IN", $day_time);
        $sqlmap['aid'] = $aid;
        $sqlmap['uid'] = $uid;

        $lists = M('DamaLog')->where($sqlmap)->order("yard_time")->getField("yard_time, id, account_id, yard_num, yard_u_coin, status, inputtime, updatetime");        
    }
    foreach ($day_time as $d) {
        $result[$d] = $lists[$d];
    }
    return $result;
}
/* 统计某工号的积分 */
function get_captcha_count($aid = 0, $uid = 0) {
    $aid = (int) $aid;
    $uid = (int) ($uid < 1) ? cookie('_userid') : $uid;

    $db = M('DamaLog');
    $sqlmap = array();
    $sqlmap['aid'] = $aid;
    $sqlmap['uid'] = $uid;

    $result = array();
    $result['all_yard_num'] = $db->where($sqlmap)->Sum('yard_num');//总积分
    $result['all_yard_u_coin'] = $db->where($sqlmap)->Sum('yard_u_coin');//总U币
    $result['yard_num'] = $db->where($sqlmap)->order("yard_time DESC")->getField('yard_num');
    $result['yard_u_coin'] = $db->where($sqlmap)->order("yard_time DESC")->getField('yard_u_coin');
    return $result;
}

function get_user_capcahe_aids($uid) {
    $db = M('DamaLog');
    $sqlmap = array();
    $sqlmap['uid'] = $uid;
    $sqlmap['status'] = 0;
    return $db->where($sqlmap)->getField('aid', TRUE);
}